<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\BaseController;
use app\common\model\Project\Order as OrderModel;

class ProjectOrder extends BaseController
{
    protected $middleware = [
        \app\admin\middleware\LoginAuth::class
    ];
    // 列表
    public function index()
    {
        $menu = [
            'name' => '预约管理',
            'title' => '预约订单'
        ];
        $searchfield = $this->request->param('search_field', 'name');
        $keyword = $this->request->param('keyword');
        $where = [];
        if ($searchfield && $keyword) {
            $where[] = [$searchfield, 'like', "%$keyword%"];
        }
        $num = 8;
        $OrderList = OrderModel::withJoin(['project', 'user','address'])->where($where)->paginate(['list_rows' => $num, 'query' => request()->param()]);
        // 当前页
        $page = $OrderList->currentPage();
        return view('', [
            'menu' => $menu,
            'OrderList' => $OrderList,
            'num' => $num,
            'page' => $page
        ]);
    }
    // 查看进度
    public function info()
    {
        $menu = [
            'name' => '预约管理',
            'title' => '预约进度'
        ];
        $orderid = $this->request->param('orderid');
        $Order = OrderModel::find($orderid);
        if (!$Order) {
            $this->error('预约订单不存在！', (string)url('/admin/projectorder/index'));
        }
        return view('', [
            'menu' => $menu,
            'Order' => $Order
        ]);
    }
    // 审核
    public function status()
    {
        $orderid = $this->request->param('orderid');
        $action = $this->request->param('action');
        $Order = OrderModel::find($orderid);
        if (!$Order) {
            $this->error('预约订单不存在！请重新选择');
        }
        if ($action == 'adopt') {
            $Order->status = -1;
            $result = $Order->save();
            if ($result === FALSE) {
                $this->error('审核通过失败');
            }
            $this->success('审核通过成功！');
        } else {
            $Order->status = -3;
            $result = $Order->save();
            if ($result === FALSE) {
                $this->error('拒绝撤销失败');
            }
            $this->success('拒绝撤销成功！');
        }
    }
    // 删除订单
    public function del()
    {
        $orderid = $this->request->param('orderid');
        $ids = $this->request->param('ids');
        $action = $this->request->param('action');
        if ($action == 'del') {
            $Order = OrderModel::find($orderid);
            if (!$Order) {
                $this->error('预约订单不存在！请重新选择');
            }
            $result=OrderModel::destroy($orderid);
            if ($result===FALSE) {
                $this->error('删除失败！');
            }
            $this->success('删除成功！');
        }
        if ($action == 'dels') {
            $OrderList = OrderModel::whereIn('id',$ids)->select();
            foreach ($OrderList as $key => $item) {
                if (!in_array($item['id'],$ids)) {
                    $this->error('所选的订单不存在！请重新选择');
                }
            }
            $result=OrderModel::destroy($ids);
            if ($result===FALSE) {
                $this->error('删除失败！');
            }
            $this->success('删除成功！');
        }
        if ($action == 'Del') {
            $Order = OrderModel::onlyTrashed()->find($orderid);
            if (!$Order) {
                $this->error('预约订单不存在！请重新选择');
            }
            $result=OrderModel::destroy($orderid,true);
            if ($result===FALSE) {
                $this->error('删除失败！');
            }
            $this->success('删除成功！');
        }
        if ($action == 'Dels') {
            $OrderList = OrderModel::onlyTrashed()->whereIn('id',$ids)->select();
            foreach ($OrderList as $key => $item) {
                if (!in_array($item['id'],$ids)) {
                    $this->error('所选的订单不存在！请重新选择');
                }
            }
            $result=OrderModel::destroy($ids,true);
            if ($result===FALSE) {
                $this->error('批量删除失败！');
            }
            $this->success('批量删除成功！');
        }
    }
    // 回收站
    public function recycle(){
        $menu = [
            'name' => '预约管理',
            'title' => '预约订单回收站'
        ];
        $searchfield = $this->request->param('search_field', 'name');
        $keyword = $this->request->param('keyword');
        $where = [];
        if ($searchfield && $keyword) {
            $where[] = [$searchfield, 'like', "%$keyword%"];
        }
        $num = 8;
        $OrderList = OrderModel::onlyTrashed()->withJoin(['project', 'user','address'])->where($where)->paginate(['list_rows' => $num, 'query' => request()->param()]);
        // 当前页
        $page = $OrderList->currentPage();
        return view('', [
            'menu' => $menu,
            'OrderList' => $OrderList,
            'num' => $num,
            'page' => $page
        ]);
    }
    // 恢复
    public function recovery(){
        $orderid = $this->request->param('orderid');
        $ids = $this->request->param('ids');
        $action = $this->request->param('action');
        if ($action == 'adopt') {
            $Order = OrderModel::onlyTrashed()->find($orderid);
            if (!$Order) {
                $this->error('预约订单不存在！请重新选择');
            }
            // 恢复
            $result=$Order->restore();
            if ($result===FALSE) {
                $this->error('恢复失败！');
            }
            $this->success('恢复成功！');
        }
        if ($action == 'adopts') {
            $OrderList = OrderModel::onlyTrashed()->whereIn('id',$ids)->select();
            foreach ($OrderList as $key => $item) {
                if (!in_array($item['id'],$ids)) {
                    $this->error('所选的订单不存在！请重新选择');
                }
            }
            $result=OrderModel::onlyTrashed()->where(['id' => $ids])->update(['deletetime' => null]);
            if ($result===FALSE) {
                $this->error('批量恢复失败！');
            }
            $this->success('批量恢复成功！');
        }
    }
}
